import {
    goodsDetail,
    commentPage,
    addCart,
    cartCount,
    settlementorder
} from '../../../api/goodsdetail.js';
import Toast from 'tdesign-miniprogram/toast/index';
Page({
    data: {
        oneHeight: 0,
        oneTop: 0,
        headerHeight: 0,
        details: {},
        navigation: {
            type: 'dots'
        },
        current: 0,
        autoplay: true,
        duration: 500,
        interval: 5000,
        goodsId: '',
        commentList: [],
        total: 0,
        count: 0
    },
    onLoad(query) {
        const {
            goodsId
        } = query;
        let menuRect = wx.getMenuButtonBoundingClientRect();
        let deviceWidth = wx.getSystemInfoSync().windowWidth;
        let rpx = (750 / deviceWidth) * Number(menuRect.height + menuRect.top);
        let headerHeight = Math.floor(rpx);
        this.setData({
            oneHeight: menuRect.height,
            oneTop: menuRect.top,
            headerHeight: headerHeight,
            goodsId: goodsId
        })
        this.getDetail(goodsId);
        this.getCartCount();
    },

    onShareAppMessage() {
        return {};
    },
    showCurImg(e) {
        const {
            index
        } = e.detail;
        const {
            images
        } = this.data.details;
        wx.previewImage({
            current: images[index],
            urls: images, // 需要预览的图片http链接列表
        });
    },
    async jumpToPeiSong() {
        const {
            details
        } = this.data
        if (details.stock == 0) {
            Toast({
                context: this,
                selector: '#t-toast',
                message: '没有库存了.',
                icon: '',
                duration: 1000,
            });
            return;
        }
        let params = {
            items: [{
                skuId: details.skus[0].id,
                count: 1
            }],
            pointStatus: wx.setStorageSync('pointStatus') || false
        }
        const res = await settlementorder(params);
        wx.setStorageSync('cartItems', params)
        wx.navigateTo({
            url: '/pages/confirmOrder/index'
        })
    },
    async getDetail(goodsId) {
        const res = await goodsDetail(goodsId);
        if (res.price) {
            res.price = (res.price / 100).toFixed(2);
        }
        if (res.description) {
            res.description = this.formatRichText(res.description)
        }
        this.getCommentPage(res.id)
        this.setData({
            details: res
        });
    },
    async getCommentPage(spuId) {
        const res = await commentPage(spuId, 0, 1, 2);
        this.setData({
            commentList: res.list,
            total: res.total
        });
    },
    async addShoppingCart() {
        if (this.data.details.stock == 0) {
            Toast({
                context: this,
                selector: '#t-toast',
                message: '没有库存了.',
                icon: '',
                duration: 1000,
            });
            return;
        }
        let skuId = this.data.details.skus[0].id
        const params = {
            skuId: skuId,
            count: 1
        }
        const res = await addCart(params)
        Toast({
            context: this,
            selector: '#t-toast',
            message: '加入成功',
            icon: '',
            duration: 1000,
        });
        this.getCartCount();
    },
    formatRichText(html) {
        let newContent = html.replace(/<img[^>]*>/gi, function (match, capture) {
            match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
            match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
            match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
            return match;
        });
        newContent = newContent.replace(/style="[^"]+"/gi, function (match, capture) {
            match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
            return match;
        });
        newContent = newContent.replace(/<br[^>]*\/>/gi, '');
        newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"');
        return newContent;
    },
    goHome() {
        wx.switchTab({
            url: '/pages/home/home',
        })
    },
    goShopCart() {
        wx.switchTab({
            url: '/pages/newCart/index',
        })
    },
    moreComment() {
        wx.navigateTo({
            url: '/pages/goods/comments/index?spuId=' + this.data.details.id,
        })
    },
    async getCartCount() {
        const res = await cartCount()
        this.setData({
            count: res
        })
    }
});